/*****************************************************************************
Description:

This program calculates the IK, CCFT, and CCFT w/ covs bandwidths

*	----------------------------------------------------------------------------
*	inputs: 		"${cleandata}data_for_bw_${year}`suffix'.dta"
*						> intermediary output from pscores_mdrd2
*					*"${cleandata}regents_cleaned_wide.dta"
*						> regents test scores
*					"${cleandata}grad_nyc_cleaned_keep_all.dta"
*						> Graduation Outcomes
*					"${cleandata}baseline_scores_standardized_grade6.dta"
*						> Baseline scores
					"${cleandata}sat_nyc_cleaned.dta"
						> SAT scores
*	----------------------------------------------------------------------------
*	outputs: 		"${cleandata}bw_for_schools_min_${year}_`grad_sep'`suffix'.dta"
*						> feed back to pscores_mdrd2
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

* Arguments
args grad_sep suffix

* Load input
use  "${cleandata}data_for_bw_${year}`suffix'.dta", clear

tostring stu, replace

* Merge demographics
merge m:1 stu using "${cleandata}demo_info_08th_grade.dta", keep(1 3) gen(demo_merge) keepusing(bl_*)
destring bl_ell bl_swd, replace

* Merge baseline scores (grade 8)
merge m:1 stu using "${cleandata}baseline_scores_standardized_grade8.dta", keep(1 3) gen(basesc_merge) keepusing(bl_ss_math bl_ss_ela)
destring stu, replace

* Merge SAT Scores
merge m:1 stu using "${cleandata}sat_nyc_cleaned.dta", keep(1 3) gen(sat_merge)

// Keep only marginal students for the bandwidths
keep if marginal == 1

* Standardize all outcomes because we're comparing across bandwidths
foreach test of varlist sat_math sat_ela sat_total bl_ss_math bl_ss_ela {
	egen mean_`test' = mean(`test')
	egen sd_`test' = sd(`test')
	gen ss_`test' = (`test' -  mean_`test') / sd_`test'
}

* Get higher order terms for baseline test scores
gen bl_ss_ela2  = bl_ss_ela^2
gen bl_ss_ela3  = bl_ss_ela^3
gen bl_ss_math2 = bl_ss_math^2
gen bl_ss_math3 = bl_ss_math^3

* Set outcomes to run for regular setup (Graduation, SAT math, SAT ela)
local tests ss_sat_math ss_sat_ela

* Indicate kids who get an offer
 egen any_offer = max(offer_mod), by(stu)
 su any_offer
 assert r(max) == 1 & r(min) == 0

// Loop over outcomes
foreach test of varlist `tests'  {

	* generate bandwidth vars
	gen ccftcovs_bwall_`test' = .

	* generate bandwidths
	su non_lotto_prg_bkt
	forval i = 1/`r(max)' {

		di in red "doing program `i' "

		* Create bandwidth for applicants who are applying to a non lotto program
		* that is fully ranked and only look at the marginal applicants
		noi cap: rdbwselect `test' hs_rank_centered if non_lotto_prg_bkt == `i' & fully_ranked_prog == 1 & marginal == 1 & indi_missing_rank_mod == 0, ///
			covs(bl_swd bl_female bl_asian bl_black bl_hispanic bl_white bl_fr_lunch bl_ss_ela bl_ss_ela2 bl_ss_ela3 bl_ss_math bl_ss_math2 bl_ss_math3) kernel(uniform)
		if _rc ==0 replace ccftcovs_bwall_`test' = e(h_mserd) if non_lotto_prg_bkt == `i'

	}

}

* Generate minimum bandwidth across the different outcomes for each non lotto program
foreach x in bwall {

	*if `grad_sep' == 0 {
	*	egen ccftcovs_`x'	  	= rowmin(ccftcovs_`x'_ss_grad_ever ccftcovs_`x'_ss_sat_math ccftcovs_`x'_ss_sat_ela)
	*	egen gradccftcovs_`x'	= rowmin(ccftcovs_`x'_ss_grad_ever ccftcovs_`x'_ss_sat_math ccftcovs_`x'_ss_sat_ela)
	*}
	*if `grad_sep' == 1 {
		egen ccftcovs_`x'	  	= rowmin(ccftcovs_`x'_ss_sat_math ccftcovs_`x'_ss_sat_ela)
	*	egen gradccftcovs_`x'  	= rowmin(ccftcovs_`x'_ss_grad_ever)
	*}

}

// Keep program ID and the bandwidths
keep non_lotto_prg_bkt prg_bkt *_bwall

// Make unique
duplicates drop
isid prg_bkt

// save
sa "${cleandata}bw_for_schools_min_${year}_`grad_sep'`suffix'.dta", replace
